package com.gmu.uav;

import java.nio.ByteBuffer;

import android.util.Log;

public class CommandPacket {
	
	private CommandCode m_CommandCode;
	private ByteBuffer m_Payload;
	private ByteBuffer m_Final_Packet;
	
	public enum CommandCode
	{
		LEFT_AILERON, RIGHT_AILERON, THRUST_CHANGE
	}
	
	/**
	*  @param commandCode The type of command to send to the UAV.
	*  @param payload     The specific data that corresponds with the command code.
	**/ 
	public CommandPacket(CommandCode commandCode, byte[] payload)
	{
		this.m_Payload = ByteBuffer.allocate(32);
		this.m_Final_Packet = ByteBuffer.allocate(33);
		
		this.m_Payload.clear();
		this.m_Final_Packet.clear();
		
		this.m_CommandCode = commandCode;
		this.m_Payload.put(payload);
		
		switch(this.m_CommandCode)
		{
			case LEFT_AILERON :
				m_Final_Packet.put((byte) 0xAA);
				break;
			case RIGHT_AILERON :
				m_Final_Packet.put((byte) 0xBB);
				break;
			case THRUST_CHANGE :
				m_Final_Packet.put((byte) 0xCC);
				break;
			default:
				Log.i("GMU ECE798 UAV", "Incorrect commandCode passed to CommandPacket class!");
				break;
		}
		
		this.m_Final_Packet.put(payload);
	}
	
	public void setCommandCode(CommandCode commandCode)
	{
		this.m_CommandCode = commandCode;
		
		m_Final_Packet.position(0);
		
		switch(commandCode)
		{
			case LEFT_AILERON :
				m_Final_Packet.put((byte) 0xAA);
				break;
			case RIGHT_AILERON :
				m_Final_Packet.put((byte) 0xBB);
				break;
			case THRUST_CHANGE :
				m_Final_Packet.put((byte) 0xCC);
				break;
			default:
				Log.i("GMU ECE798 UAV", "Incorrect commandCode passed to CommandPacket class!");
				break;
		}
	}
	
	public void setPayload(byte[] payload)
	{
		this.m_Payload.clear();
		this.m_Payload.position(0);
		
		this.m_Payload.put(payload);
		
		this.m_Final_Packet.position(1);
		this.m_Final_Packet.put(payload);
		
	}
	
	public byte[] getCommandBytes()
	{	
		return this.m_Final_Packet.array();
	}
}